package com.rzdata.poct.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rzdata.poct.common.core.mybatisplus.core.ServicePlusImpl;
import com.rzdata.poct.common.core.page.PagePlus;
import com.rzdata.poct.common.core.page.TableDataInfo;
import com.rzdata.poct.common.utils.PageUtils;
import com.rzdata.poct.common.utils.StringUtils;
import com.rzdata.poct.system.domain.business.bo.PoctMaterialLogsBo;
import com.rzdata.poct.system.domain.business.po.PoctMaterialLogs;
import com.rzdata.poct.system.domain.business.po.PoctMaterialStore;
import com.rzdata.poct.system.domain.business.vo.PoctMaterialLogsVo;
import com.rzdata.poct.system.mapper.PoctMaterialLogsMapper;
import com.rzdata.poct.system.service.IPoctMaterialLogsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author ruoyi
 * @date 2021-09-09
 */
@Service
public class PoctMaterialLogsServiceImpl extends ServicePlusImpl<PoctMaterialLogsMapper, PoctMaterialLogs, PoctMaterialLogsVo> implements IPoctMaterialLogsService {

    @Autowired
    private PoctMaterialLogsMapper poctMaterialLogsMapper;

    @Override
    public PoctMaterialLogsVo queryById(Long id) {
        return getVoById(id);
    }

    @Override
    public TableDataInfo<PoctMaterialLogsVo> queryPageList(PoctMaterialLogsBo bo) {
        PagePlus<PoctMaterialLogs, PoctMaterialLogsVo> result = pageVo(PageUtils.buildPagePlus(), buildQueryWrapper(bo));
        return PageUtils.buildDataInfo(result);
    }

    @Override
    public List<PoctMaterialLogsVo> queryList(PoctMaterialLogsBo bo) {
        return listVo(buildQueryWrapper(bo));
    }

    private LambdaQueryWrapper<PoctMaterialLogs> buildQueryWrapper(PoctMaterialLogsBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<PoctMaterialLogs> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getMaterialId() != null, PoctMaterialLogs::getMaterialId, bo.getMaterialId());
        lqw.eq(StringUtils.isNotBlank(bo.getMaterialNo()), PoctMaterialLogs::getMaterialNo, bo.getMaterialNo());
        lqw.eq(bo.getProjectId() != null, PoctMaterialLogs::getProjectId, bo.getProjectId());
        lqw.like(StringUtils.isNotBlank(bo.getProjectName()), PoctMaterialLogs::getProjectName, bo.getProjectName());
        lqw.eq(StringUtils.isNotBlank(bo.getProjectNo()), PoctMaterialLogs::getProjectNo, bo.getProjectNo());
        lqw.eq(bo.getDeptId() != null, PoctMaterialLogs::getDeptId, bo.getDeptId());
        lqw.eq(StringUtils.isNotBlank(bo.getBatchNo()), PoctMaterialLogs::getBatchNo, bo.getBatchNo());
        lqw.eq(bo.getUseTime() != null, PoctMaterialLogs::getUserTime, bo.getUseTime());
        lqw.eq(StringUtils.isNotBlank(bo.getUser()), PoctMaterialLogs::getUser, bo.getUser());
        return lqw;
    }

    @Override
    public Boolean insertByBo(PoctMaterialLogsBo bo) {
        PoctMaterialLogs add = BeanUtil.toBean(bo, PoctMaterialLogs.class);
        validEntityBeforeSave(add);
        return save(add);
    }

    @Override
    public Boolean updateByBo(PoctMaterialLogsBo bo) {
        PoctMaterialLogs update = BeanUtil.toBean(bo, PoctMaterialLogs.class);
        validEntityBeforeSave(update);
        return updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(PoctMaterialLogs entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }

    @Override
    public TableDataInfo<PoctMaterialLogsVo> queryPageMaterialLogsList(PoctMaterialLogsBo bo) {
//        Page<PoctMaterialLogs> buildPage = PageUtils.buildPage();
//        buildPage.setSize(bo.getPageSize()).setCurrent(bo.getPageNum());
        return PageUtils.buildDataInfo(poctMaterialLogsMapper.queryPageMaterialLogsList(PageUtils.buildPage(), bo));
    }
}
